#include "ThreadController.h"
#include <iostream>
ThreadController::ThreadController(QObject *parent) : QObject(parent)
{

    qDebug() << "Controller's thread is :" << QThread::currentThreadId();
//    account = Account::getInstance();
    ui_manager = UIManager::getInstance();
    ui_thread =new QThread();
//    ui_manager->moveToThread(ui_thread);
    friendnum=0;


    connect(this, &ThreadController::startRunning, ui_manager, &UIManager::run);
    connect(ui_thread, &QThread::finished, ui_manager, &QObject::deleteLater);
    connect(ui_manager, &UIManager::resultReady, this, &ThreadController::on_receivResult);
    ui_thread->start();

    server_message_adapter = ServerMessageAdapter::getInstance();
//    server_message_adapter->setAccount(account);
    back_thread = new QThread();

    server_message_adapter->moveToThread(back_thread);

//    connect(this, &ThreadController::startRunning, server_message_adapter,&ServerMessageAdapter::run);
    connect(TCPSocket::getInstance(),SIGNAL(readyRead()),server_message_adapter,SLOT(solveServerMessage()));

    connect(back_thread, &QThread::finished, server_message_adapter, &QObject::deleteLater);
    connect(server_message_adapter, &ServerMessageAdapter::resultReady, this, &ThreadController::on_receivResult);
    back_thread->start();
     friend_list=friendlist::getinstance();
     enroll_window=enrollwindow::getinstance();
//      main_chat=new chatwindow(100);
    login_window=new Loginwindow();
    myfindpassword=findpassword::getinstance();
    myreset=reset::getinstance();

    mydialog=Dialog::getinstance();
    mysendcheck=sendcheck::getinstance();
    myinfo=InfoForm::getinstance();
    initBindTask();



}
void ThreadController::initBindTask()
{
    connect(login_window,SIGNAL(openfriendlist()),friend_list,SLOT(show()));
    connect(login_window,SIGNAL(sendid(QString)),this,SLOT(getmyid(QString)));
    connect(login_window,SIGNAL(sig_task_login_start(QString ,QString)),ui_manager,SLOT(addTaskLogin(QString,QString)));
   connect(login_window,SIGNAL(openenroll()),enroll_window,SLOT(show()));

   connect(myfindpassword,SIGNAL(openreset()),myreset,SLOT(show()));
    connect(UIManager::getInstance(),SIGNAL(sig_login(Package::LoginRequestSend)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::LoginRequestSend)),Qt::DirectConnection);



    connect(friend_list,SIGNAL(openchat(QString)),this,SLOT(newfriend(QString)));




    connect(login_window,SIGNAL( openpassword()),myfindpassword,SLOT(show()));
    connect(ServerMessageAdapter::getInstance(),SIGNAL(sig_LOGIN_SUCCESS(bool)),login_window,SLOT(open(bool)));
    connect(myfindpassword,SIGNAL(sendcode(QString)),UIManager::getInstance(),SLOT(addphone(QString)));
    connect(ServerMessageAdapter::getInstance(),SIGNAL(sig_LOGIN_INFO(QString,QString,QString)),UIManager::getInstance(),SLOT(LoginSuccess(QString,QString,QString)));



    connect(ServerMessageAdapter::getInstance(),SIGNAL(sig_LIST_SYNC()),friend_list,SLOT(getIdAndName()));
//    connect(UIManager::getInstance(),SIGNAL(sig_InfoSync()),friend_list,SLOT(getIdAndName()));


    connect(UIManager::getInstance(),SIGNAL(sig_test(Package::RegisterRequestSend)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::RegisterRequestSend)),Qt::DirectConnection);
    connect(UIManager::getInstance(),SIGNAL(sig_test(Package::LoginRequestSend)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::LoginRequestSend)),Qt::DirectConnection);
    connect(enroll_window,SIGNAL(sendphone(QString)),UIManager::getInstance(),SLOT(addphone(QString)));
    connect(UIManager::getInstance(),SIGNAL(sig_phone_check(Package::ChecknumRequestSend)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::ChecknumRequestSend)),Qt::DirectConnection);
    connect(enroll_window,SIGNAL(sendUseInfo(QString,QString,QString,QString)),UIManager::getInstance(),SLOT( registerRequest(QString,QString,QString,QString)));
    connect(UIManager::getInstance(),SIGNAL(sig_register(Package::RegisterRequestSend)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::RegisterRequestSend)),Qt::DirectConnection);
    connect(ServerMessageAdapter::getInstance(),SIGNAL(sig_REIGSTER_RES(QString)),enroll_window,SLOT(SuccessAndgetMyId(QString)));
    qDebug()<<"bind task success";


    connect(friend_list,SIGNAL(ToAddFriend()),mydialog,SLOT(show()));
    connect(mydialog,SIGNAL(addfriend(QString)),UIManager::getInstance(),SLOT(addFriend(QString)));
    connect(UIManager::getInstance(),SIGNAL(sig_add_friend(Package::AddFriendRequestSend)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::AddFriendRequestSend)),Qt::DirectConnection);

//    connect(UIManager::getInstance(),SIGNAL(sig_InfoSync()),ServerMessageAdapter::getInstance(),SLOT(sendInfoSyncMessage()));
    connect(UIManager::getInstance(),SIGNAL(sig_send_msg(Package::MessageSend)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::MessageSend)),Qt::DirectConnection);
    connect(UIManager::getInstance(),SIGNAL(sig_sync_msg(Package::MessageHistoryRequestSend)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::MessageHistoryRequestSend)),Qt::DirectConnection);






    connect(ServerMessageAdapter::getInstance(),SIGNAL(sig_MSG_SYNC(QString,QList<QPair<QString,QString>>)),this,SLOT(GetChatHistory(QString,QList<QPair<QString,QString>>)),Qt::DirectConnection);
connect(ServerMessageAdapter::getInstance(),SIGNAL(sig_MSG_SYNC(QString,QList<QPair<QString,QString>>)),this,SLOT(GetGroupHistroy(QString,QList<QPair<QString,QString>>)),Qt::DirectConnection);





    connect(ServerMessageAdapter::getInstance(),SIGNAL(sig_MSG_ACC(QString,QString)),this,SLOT(GetFriendMsg(QString,QString)));

    connect(UIManager::getInstance(),SIGNAL(sig_agree_friend(Package::AgreeToAddFriendStatus)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::AgreeToAddFriendStatus)),Qt::DirectConnection);
    connect(UIManager::getInstance(),SIGNAL(sig_agree_group(Package::AgreeToGroupStatus)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::AgreeToGroupStatus)),Qt::DirectConnection);

    connect(ServerMessageAdapter::getInstance(),SIGNAL(sig_MSG_ACC(QString,QString)),this,SLOT(GetFriendMsg(QString,QString)));

    connect(login_window,SIGNAL(sendcheck()),mysendcheck,SLOT(show()));
    connect(mysendcheck,SIGNAL(openflist(bool)),login_window,SLOT(open(bool)));
    connect(ServerMessageAdapter::getInstance(),SIGNAL(sig_POST_SYNC()),myinfo,SLOT(getinfo()));
    connect(myinfo,SIGNAL(recovery(QString,QString,bool)),UIManager::getInstance(),SLOT(agreeToGroup(QString,QString,bool)));

connect(ServerMessageAdapter::getInstance(),SIGNAL(sig_LIST_SYNC()),friend_list,SLOT(getGroupIdAndName()));

connect(friend_list,SIGNAL(creategroup()),UIManager::getInstance(),SLOT(createGroup()));
connect(friend_list,SIGNAL(sendgroupid(QString)),this,SLOT(newgroup(QString)));
connect( ServerMessageAdapter::getInstance(),SIGNAL(sig_MSG_ACC(QString,QString,QString)),this,SLOT(GetGroupInfo(QString ,QString ,QString )));
connect(UIManager::getInstance(),SIGNAL(sig_create_group(Package::CreateGroup)),ServerMessageAdapter::getInstance(),SLOT(sendMessage(Package::CreateGroup)),Qt::DirectConnection);
}

void ThreadController::start()
{
    TCPSocket::getInstance()->TCPconnect();
//    qDebug()<<"ThreadController start";
//    emit startRunning();
//    Package::ChecknumRequestSend package{"11111111"};
//    emit sig_phone_check(package);

//    w.show();
    login_window->setWindowTitle("登录界面");
    login_window->show();
//
}
void ThreadController::on_receivResult()
{
//    this->count++;
//    qDebug()<<"get result from threadController receive message ";



}
void ThreadController::newfriend(QString id){
if(!main_chat.contains(id)){
    qDebug()<<"132";
    main_chat.insert(id,new chatwindow());
    main_chat[id]->friendid=id;
    main_chat[id]->myid=myid;
    connect(main_chat[id],SIGNAL(sendmsg(QString,QString)),UIManager::getInstance(),SLOT(sendMessage(QString,QString)));

    connect(this,SIGNAL(sig_msg_requeset(QString)),UIManager::getInstance(),SLOT(msgSyncRequest(QString )));

    emit sig_msg_requeset(id);
    main_chat[id]->show();
    //connect();
      friendnum++;
//8276757830
 qDebug()<<"143";
}
else{
if(main_chat[id]!=nullptr){

  main_chat[id]->show();
}
else{
  main_chat[id]=new chatwindow();


}

}


}
void ThreadController::GetChatHistory(QString id,QList<QPair<QString,QString>> history){
//QString gid="";
//Account::getInstance()->getGid(id,gid);
    qDebug()<<"1234";
    qDebug()<<"511"<<id;
    if(main_chat.contains(id))
    main_chat[id]->gethistory(history);
    qDebug()<<"123477777";

}
void ThreadController::getmyid(QString id){

 myid=id;
}



void ThreadController::GetFriendMsg(QString uid ,QString msg){

    QString gid="";
    Account::getInstance()->getGid(uid,gid);
     main_chat[gid]->GetFriendMsg(msg);



}

//void ThreadController::GetFriendMsg(QString id ,QString msg){
//     main_chat[id]->GetFriendMsg(msg);
//}



void ThreadController::newgroup(QString id){
    if(!group_chat.contains(id)){
        group_chat.insert(id,new groupchat());

        group_chat[id]->myid=myid;
       group_chat[id]->groupid=id;
//UIManager::sendMessageGroup(QString gid,QString uid,QString msg)
  connect(group_chat[id],SIGNAL(sendGroupMsg(QString ,QString ,QString )), UIManager::getInstance(),SLOT(sendMessageGroup(QString ,QString ,QString )));
  connect(this,SIGNAL(sig_msg_requeset(QString)),UIManager::getInstance(),SLOT(msgSyncRequest(QString )));
 emit sig_msg_requeset(id);
qDebug()<<"connect group";

         group_chat[id]->show();

    }
    else{
        if(group_chat[id]==nullptr){

           group_chat[id]=new groupchat();

        }

            group_chat[id]->show();




    }





}



void ThreadController::GetGroupHistroy(QString id,QList<QPair<QString,QString>> history){
qDebug()<<"GetGroupHistroy()";
if(group_chat.contains(id))
    if(group_chat[id]==nullptr)
        group_chat[id]=new groupchat();
 qDebug()<<"get group his success2";
    group_chat[id]->getgrouphistory(history);
    qDebug()<<"get group his success";

}
void ThreadController::GetGroupInfo(QString gid,QString uid,QString info){

    if(group_chat.contains(gid)){
        if(group_chat[gid]==nullptr){

            group_chat[gid]=new groupchat();
        }
       group_chat[gid]->GetGroupInfo(gid,uid,info);

    }



}

